package com.exam.master.model.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.bean.UserInfo;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.master.R;
import com.exam.master.databinding.ActivityRegisterBinding;
import com.exam.commonbiz.model.IRegisterCallBack;
import com.exam.commonbiz.viewmodel.CommonViewModel;
import com.sanshao.commonui.titlebar.OnTitleBarListener;

public class RegisterActivity extends BaseActivity<CommonViewModel, ActivityRegisterBinding> implements IRegisterCallBack {

    public static void start(Context context) {
        Intent starter = new Intent(context, RegisterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initData() {

        binding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        mViewModel.setIRegisterCallBack(this);
        binding.btnSubmit.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                String phone = binding.edtPhone.getText().toString();
                String psd = binding.edtPsd.getText().toString();
                String psd2 = binding.edtPsd2.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showLongToastCenter("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(psd) || TextUtils.isEmpty(psd2)) {
                    ToastUtil.showLongToastCenter("密码不能为空");
                    return;
                }
                if (!TextUtils.equals(psd, psd2)) {
                    ToastUtil.showLongToastCenter("两次密码不一致");
                    return;
                }
                UserInfo userInfo = new UserInfo();
                userInfo.phone = phone;
                userInfo.password = psd;
                mViewModel.register(userInfo);
            }
        });

        binding.tvToLogin.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                finish();
            }
        });
    }

    @Override
    public int getStatusBarColor() {
        return R.color.color_bg;
    }

    @Override
    public void onRegisterSuccess() {
        ToastUtil.showLongToastCenter("注册成功");
        finish();
    }
}